package com.cloud.wms.ui.inside.transfer;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.wms.R;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.config.SpKey;
import com.cloud.wms.ui.inside.contract.TransferDownContact;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.inside.presenter.TransferDownPresenter;
import com.cloud.wms.ui.sort.entity.DetachBoxBean;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FC on 2021/5/18.
 * Desc：移库下架确认
 */
public class TransferDownConfirmActivity extends BaseBarActivity<TransferDownPresenter> implements TransferDownContact.View {
    @BindView(R.id.tv_bar_code)
    TextView tvBarCode;
    @BindView(R.id.tv_commodity_name)
    TextView tvCommodityName;
    @BindView(R.id.tv_pos_code)
    TextView tvPosCode;
    @BindView(R.id.tv_production_date)
    TextView tvProductionDate;
    @BindView(R.id.tv_expire_date)
    TextView tvExpireDate;
    @BindView(R.id.tv_in_date)
    TextView tvInDate;
    @BindView(R.id.tv_stock_type)
    TextView tvStockType;
    @BindView(R.id.tv_all_amount)
    TextView tvAllAmount;
    @BindView(R.id.et_amount)
    EditText etAmount;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    @BindView(R.id.tv_pos_title)
    TextView tvPosTitle;
    @BindView(R.id.tv_amount_title)
    TextView tvAmountTitle;
    @BindView(R.id.tv_et_amount_title)
    TextView tvEtAmountTitle;
    @BindView(R.id.tv_container_title)
    TextView tvContainerTitle;
    @BindView(R.id.tv_container_code)
    TextView tvContainerCode;
    TransferCommodityEntity commodityEntity;
    private DetachBoxBean boxBean;

    @Override
    protected String barTitle() {
        return "移库下架";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_transfer_down_confirm;
    }

    @Override
    protected void initEvent() {
        commodityEntity = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);
        boxBean = getIntent().getParcelableExtra(IntentKey.INFO_KEY);
        tvBarCode.setText(commodityEntity.getBarCode());
        tvCommodityName.setText(commodityEntity.getCommodityName());
        tvPosCode.setText(commodityEntity.getPosCode());
        tvProductionDate.setText(commodityEntity.getProductionDate());
        tvExpireDate.setText(commodityEntity.getExpirationDate());
        tvInDate.setText(commodityEntity.getInDate());
        tvStockType.setText(getStockType(commodityEntity.getStockType()));
        tvAllAmount.setText(String.valueOf(commodityEntity.getAmount()));
        if (FineExApplication.component().kv().decodeBool(SpKey.TRANSFER_SCAN_CONTAINER) && boxBean != null) {
            tvContainerTitle.setVisibility(View.VISIBLE);
            tvContainerCode.setVisibility(View.VISIBLE);
            tvContainerCode.setText(boxBean.getContainerCode());
        }
        etAmount.requestFocus();
    }

    @Override
    public void onSuccess(Message message) {
        if (message.what == TransferDownPresenter.CONFIRM_DOWN) {
            success("下架成功");
            EventBusUtil.sendEvent(new Event<>(EventConfig.CLOSE_REFRESH));
            new AlertDialogBuilder(this)
                    .setCancelable(false)
                    .setTitle("上架提示")
                    .setMessage("是否立即上架该商品？")
                    .setNegativeButton("继续下架", (dialog, which) -> finish())
                    .setPositiveButton("确定上架", (dialog, which) -> {
                        startActivity(new Intent(TransferDownConfirmActivity.this, TransferUpStartActivity.class));
                        finish();
                    })
                    .show();

        }
    }

    @OnClick(R.id.btn_confirm)
    public void onClick() {
        if (TextUtils.isEmpty(etAmount.getText().toString())) {
            info("下架数量不能为空");
            return;
        }
        if (commodityEntity.getAmount() < Integer.parseInt(etAmount.getText().toString())) {
            info("下架数量不能大于可下架数");
            return;
        }
        mPresenter.confirmDown(commodityEntity, etAmount.getText().toString(), boxBean);
    }

    public static void jumpToNext(Activity activity, TransferCommodityEntity commodityEntity, DetachBoxBean boxBean) {
        Intent intent = new Intent(activity, TransferDownConfirmActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, commodityEntity);
        intent.putExtra(IntentKey.INFO_KEY, boxBean);
        activity.startActivity(intent);
    }

    /**
     * @param stockType 1良品 2次品 3废品
     * @return
     */
    private String getStockType(int stockType) {
        if (stockType == 2)
            return "次品";
        else if (stockType == 3)
            return "废品";
        else
            return "良品";
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
